home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / DRAG_CON.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  12.3 KB  |  359 lines

  1.  
  2. package sub_arctic.lib;
  3.  
  4. import sub_arctic.lib.sub_arctic_error;
  5. import sub_arctic.output.loaded_image;
  6. import sub_arctic.output.drawable;
  7. import sub_arctic.input.pressable;
  8. import sub_arctic.input.move_draggable;
  9. import sub_arctic.input.event;
  10. import sub_arctic.input.user_info_holder;
  11. import sub_arctic.input.pick_collector;
  12. import sub_arctic.input.callback_object;
  13. import java.awt.Point;
  14.  
  15. /** 
  16.  * This class provides a container for dragging.  You can put any subtree
  17.  * inside it to make that subtree draggable.  The container handles the
  18.  * pick process correctly so that it only initiates a drag if you actually
  19.  * click over an object inside the an object in the sub-tree (not simply
  20.  * anywhere inside the bounds of the container).  The container will 
  21.  * "shrink-wrap" around the objects it contains, and can optionally draw
  22.  * a bounding rectangle as drag feedback.
  23.  *
  24.  * @author Scott Hudson
  25.  */
  26. public class drag_container extends shrink_wrap_container 
  27. implements pressable, move_draggable {
  28.  
  29.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  30.  
  31.   /** Flag bit position for flag indicating whether to do bounding box
  32.    *  drag feedback. */
  33.   protected static final int DO_BB_FEEDBACK = DRAW_BORDER << 1;
  34.  
  35.  
  36.   /** Is the object currently set to do bounding box drag feedback? */
  37.   public boolean bounding_box_feedback() {return flag_is_set(DO_BB_FEEDBACK);}
  38.  
  39.  
  40.   /** 
  41.    * Set object to do or not do bounding box drag feedback. 
  42.    *
  43.    * @param boolean do_bbf passed true if the object should do bounding box 
  44.    *                       feedback during drag.
  45.    */
  46.   public void set_bounding_box_feedback(boolean do_bbf) 
  47.     {
  48.       if (flag_is_set(DO_BB_FEEDBACK) != do_bbf)
  49.     {
  50.           set_flag_bit(DO_BB_FEEDBACK, do_bbf); 
  51.       damage_self();
  52.     }
  53.     }
  54.  
  55.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  56.  
  57.   /** 
  58.    * Flag bit position for flag indicating whether we are currently in the
  59.    * middle of a drag. 
  60.    */
  61.   protected static final int DOING_DRAG = DO_BB_FEEDBACK << 1;
  62.  
  63.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  64.  
  65.   /** Object we make callbacks to.  */
  66.   protected callback_object _callback_obj = null;
  67.  
  68.   /** 
  69.    * Retrieve the object we make callbacks to.  
  70.    * @return callback_object the callback object.
  71.    */
  72.   public callback_object callback_obj() {return _callback_obj;}
  73.  
  74.   /** 
  75.    * Set the object we make callbacks to.  
  76.    * @param callback_object cbo the new callback object.
  77.    */
  78.   public void callback_obj(callback_object cbo) {_callback_obj = cbo;}
  79.  
  80.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  81.  
  82.   /** 
  83.    * Constant for "move" callback (called on every move, including last). 
  84.    * This callback provides a Point object with the position of the container
  85.    * in its parent's coordinates.
  86.    */
  87.   public final static int MOVE_CALLBACK = 0;
  88.  
  89.   /** 
  90.    * Constant for "end" callback (called only at end of move). 
  91.    * This callback provides a Point object with the position of the container
  92.    * in its parent's coordinates.
  93.    */
  94.   public final static int END_MOVE_CALLBACK = 1;
  95.    
  96.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  97.  
  98.   /** 
  99.    * full constructor.  
  100.    *
  101.    * @param int             x              initial x position of the container.
  102.    * @param int             y              initial y position of the container.
  103.    * @param boolean         do_bb_feedback whether we do bounding box feedback.
  104.    * @param callback_object cbo            object to make callbacks to.
  105.    */
  106.   public drag_container(
  107.     int x, int y, boolean do_bb_feedback, callback_object cbo) 
  108. {
  109.       super(x,y,2,false);
  110.       set_bounding_box_feedback(do_bb_feedback);
  111.       set_flag_bit(DOING_DRAG, false);
  112.       _callback_obj = cbo;
  113.     }
  114.  
  115.    //had:
  116.    //* @exception general
  117.  
  118.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  119.  
  120.   /** 
  121.    * Nearly full constructor.  This provides a null callback.
  122.    *
  123.    * @param int     x              initial x position of the container.
  124.    * @param int     y              initial y position of the container.
  125.    * @param boolean do_bb_feedback whether we do bounding box feedback.
  126.    */
  127.   public drag_container(int x, int y, boolean do_bb_feedback) 
  128. {
  129.       super(x,y,2,false);
  130.       set_bounding_box_feedback(do_bb_feedback);
  131.       set_flag_bit(DOING_DRAG, false);
  132.       _callback_obj = null;
  133.     }
  134.  
  135.    //had:
  136.    //* @exception general
  137.  
  138.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  139.  
  140.   /** 
  141.    * Handle mouse button press input to the object.  Here, we start dragging. 
  142.    * @param event  evt press event being dispatched.
  143.    * @param Object user_info information provided by this object at pick time 
  144.    *                         (in this case ignored).
  145.    */
  146.   public boolean press(event evt, Object user_info)
  147.     {
  148.       manager.move_drag_focus.set_focus_to(this, evt, new point_info(0,0));
  149.       return true;
  150.     }
  151.  
  152.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  153.  
  154.   /** 
  155.    * Companion to press().  Here we ignore this. 
  156.    * @param event  evt release event being dispatched.
  157.    * @param Object user_info information provided by this object at pick time 
  158.    */
  159.   public boolean release(event evt, Object user_info)
  160.     {
  161.       return false;
  162.     }
  163.  
  164.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  165.  
  166.   /** 
  167.    * Handle the start of a drag to the object.  
  168.    * @param event evt   the event "causing" the start of the drag.
  169.    * @param int    x_pos     x position of new position (in parent's coords).
  170.    * @param int    y_pos     y position of new position (in parent's coords).
  171.    * @param int    grab_x    x position where we started the drag (in local 
  172.    *                         coords).
  173.    * @param int    grab_y    y position where we started the drag (in local 
  174.    *                         coords).
  175.    * @param Object user_info information provided when this object requested 
  176.    *                         focus.
  177.    * @return boolean indicating whether the input was consumed (in this case it
  178.    *                 always is).
  179.    */
  180.   public boolean drag_start(
  181.     event evt, 
  182.     int x_pos, int y_pos, 
  183.     int grab_x, int grab_y, 
  184.     Object user_info)
  185.     {
  186.       /* position at x,y */
  187.       set_pos(x_pos, y_pos);
  188.  
  189.       /* remember we are dragging */
  190.       set_flag_bit(DOING_DRAG, true);
  191.  
  192.       /* if we are doing bounding box feedback force a redraw regardless */
  193.       if (bounding_box_feedback()) damage_self();
  194.  
  195.       return true;
  196.     }
  197.  
  198.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  199.  
  200.   /** 
  201.    * Handle a movement during a drag.  Here we just set our position to 
  202.    * follow the event location and do the move callback. 
  203.    *
  204.    * @param event evt   the event "causing" the start of the drag.
  205.    * @param int    x_pos     x position of new position (in parent's coords).
  206.    * @param int    y_pos     y position of new position (in parent's coords).
  207.    * @param int    start_x   x position where we started the drag (in parent's
  208.    *                         coords).
  209.    * @param int    start_y   y position where we started the drag (in parent's
  210.    *                         coords).
  211.    * @param int    grab_x    x position where we started the grab (in local 
  212.    *                         coords).
  213.    * @param int    grab_y    y position where we started the grab (in local 
  214.    *                         coords).
  215.    * @param Object user_info information provided when this object requested 
  216.    *                         focus.
  217.    * @return boolean indicating whether the input was consumed (in this case it
  218.    *                 always is).
  219.    */
  220.   public boolean drag_feedback(
  221.     event evt, 
  222.     int x_pos, int y_pos, 
  223.     int st_x, int st_y, 
  224.     int grab_x, int grab_y, 
  225.     Object user_info)
  226.     {
  227.       /* move our position */
  228.       set_pos(x_pos, y_pos);
  229.       
  230.       /* do callback */
  231.       if (callback_obj() != null) {
  232.     callback_obj().callback(this, evt, MOVE_CALLBACK, 
  233.                 new Point(x_pos, y_pos));
  234.       }
  235.       return true;
  236.     }
  237.  
  238.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  239.  
  240.   /** 
  241.    * Handle input corresponding to the end of a drag.  
  242.    *
  243.    * @param event evt   the event "causing" the start of the drag.
  244.    * @param int    x_pos     x position of new position (in parent's coords).
  245.    * @param int    y_pos     y position of new position (in parent's coords).
  246.    * @param int    start_x   x position where we started the drag (in parent's
  247.    *                         coords).
  248.    * @param int    start_y   y position where we started the drag (in parent's
  249.    *                         coords).
  250.    * @param int    grab_x    x position where we started the grab (in local 
  251.    *                         coords).
  252.    * @param int    grab_y    y position where we started the grab (in local 
  253.    *                         coords).
  254.    * @param Object user_info information provided when this object requested 
  255.    *                         focus.
  256.    * @return boolean indicating whether the input was consumed (in this case it
  257.    *                 always is).
  258.    */
  259.   public boolean drag_end(
  260.     event evt, 
  261.     int x_pos, int y_pos, 
  262.     int st_x, int st_y, 
  263.     int grab_x, int grab_y, 
  264.     Object user_info)
  265.     {
  266.       boolean result = false;
  267.  
  268.       /* we are done dragging */
  269.       set_flag_bit(DOING_DRAG, false);
  270.       damage_self();
  271.  
  272.       /* let drag_feedback do most of the work */
  273.       result = drag_feedback(evt, x_pos, y_pos, st_x, st_y, grab_x, grab_y, 
  274.                  user_info);
  275.       /* do ending callback */
  276.       if (callback_obj() != null)
  277.     callback_obj().callback(this, evt, END_MOVE_CALLBACK, 
  278.                 new Point(x_pos, y_pos));
  279.       return result;
  280.     }
  281.  
  282.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  283.  
  284.   /** 
  285.    * Determine if this object is "picked" by the the given point.  In this
  286.    * case, we are only picked if at least one of our descendents is picked,
  287.    * but we need to be in the pick list before our children so we can 
  288.    * intercept input intended for them.  To do this, we do a "local" pick
  289.    * within our children, then merge that back into the "real" pick as needed.
  290.    * putting ourselves first then picks from our children.
  291.    *
  292.    * @param int            pt_x      x of point we are testing for pick.
  293.    * @param int            pt_y      y of point we are testing for pick.
  294.    * @param pick_collector pick_list the collector for our result.
  295.    */
  296.   public void pick(int pt_x, int pt_y, pick_collector pick_list) 
  297. {
  298.       pick_collector   local_pick;
  299.       user_info_holder info;
  300.  
  301.       /* don't pick anything unless we are enabled and its inside our bounds */
  302.       if (enabled() && visible() && picked_by(pt_x, pt_y))
  303.     {
  304.       /* create a new "local" pick collector an do child picks with that */
  305.       local_pick = new pick_collector();
  306.           pick_within_children(pt_x, pt_y, local_pick);
  307.  
  308.       /* insert picks (if we have any) in "real" pick list */
  309.       if (local_pick.num_picks() > 0)
  310.         {
  311.           /* we go before our children */
  312.           pick_list.report_pick(this);
  313.  
  314.           /* report picks from children in order */
  315.           for (int i = 0; i<local_pick.num_picks(); i++)
  316.         {
  317.           info = local_pick.pick(i);
  318.           pick_list.report_pick(info.obj, info);
  319.         }
  320.         }
  321.     }
  322.     }
  323.  
  324.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  325.  
  326.   /** 
  327.    * Draw the object's current appearance.  This draws the children
  328.    * and then the optional border rectangle if we are being dragged.
  329.    * @param drawable d the surface we draw on.
  330.    */
  331.   protected void draw_self_local(drawable d) 
  332. {
  333.       /* let superclass draw any children we have */
  334.       super.draw_self_local(d);
  335.  
  336.       /* optionally draw our border */
  337.       if (bounding_box_feedback() && flag_is_set(DOING_DRAG))
  338.         d.drawRect(0,0,w()-1,h()-1);
  339.     }
  340.  
  341.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  342. }
  343. /*=========================== COPYRIGHT NOTICE ===========================
  344.  
  345. This file is part of the subArctic user interface toolkit.
  346.  
  347. Copyright (c) 1996 Scott Hudson and Ian Smith
  348. All rights reserved.
  349.  
  350. The subArctic system is freely available for most uses under the terms
  351. and conditions described in 
  352.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  353. and appearing in full in the lib/interactor.java source file.
  354.  
  355. The current release and additional information about this software can be 
  356. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  357.  
  358. ========================================================================*/
  359.